package io.nutz.nutzsite.common.socket;

import java.util.Set;

import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.lang.util.NutMap;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.plugins.mvc.websocket.handler.SimpleWsHandler;

@IocBean
public class WkWsHandler extends SimpleWsHandler {
    private static final Log log = Logs.get();
    protected Set<String> rooms;
    @Inject
    private WkNotifyService wkNotifyService;
    
    public WkWsHandler(){
    	super("");// 覆盖默认前缀
    }

    public void init() {
        super.init(); // 必须调用超类的init,除非直接实现WsHandler接口
/*        if (httpSession != null)
            httpSession.setAttribute("wsid", session.getId()); // 其他业务代码只需要从HttpSession取出wsid,即可调用AbstractWsEndpoint的api发送消息
*/    }
    /**
     * 获取通知 对应的消息是  {action:"msg", room:"superadmin"}
     */
    public void msg(NutMap req) {
        msg(req.getString("room"));
    }

    public void msg(String room) {
        if (!Strings.isBlank(room)) {
            wkNotifyService.getMsg(room);
        }
    }
    /**
     * 发送消息到房间
     * @param req
     */
    public void msgRoom(final NutMap req) {
        String room =  req.getString("room");
        msg2room(req);
    }
    /**
     * 自定义方法
     * @param req
     * 前端使用样例
     * ws.send(JSON.stringify({"action":"sayhi", "nickname" : "wendal"}));
     */
    public void sayhi(NutMap req) { // 对应js端的action名称,方法参数必须是NutMap哦
        String name = req.getString("nickname");// 可以拿到页面发过来的任意内容
        NutMap resp = new NutMap("action", "notify"); // 响应的内容完全由你决定,推荐用{action:"xxx", ....}
        resp.setv("msg", "湖南人文科技学院, " + name);
        endpoint.sendJson(session.getId(), resp); // 通过endpoint可以发生给任何你想发生的对象, session就是当前WebSocket的会话.
    }
    /**
     * 加入房间 对应的消息是  {action:"join", room:"wendal"}
     */
    @Override
    public void join(NutMap req) {
    	String roomname=req.getString("room");    	
        join(roomname);
    }

    /**
     * 退出房间 对应的消息是 {action:"left", room:"wendal"}
     */
    @Override
    public void left(NutMap req) {
        left(req.getString("room"));
    }

    @Override
    public void join(String room) {
        if (!Strings.isBlank(room)) {
            room = prefix + room;
            log.debugf("\r\nsession(id=%s) join room(name=%s)", session.getId(), room);
            roomProvider.join(room, session.getId());
            if (httpSession != null){
                httpSession.setAttribute("wsid", session.getId());
            }
        }
    }

    @Override
    public void left(String room) {
        if (!Strings.isBlank(room)) {
            room = prefix + room;
            log.debugf("session(id=%s) left room(name=%s)", session.getId(), room);
            roomProvider.left(room, session.getId());
        }
    }

    @Override
    public void depose() {
        //覆盖原生写法,因为room= loginname + httpSessionId 和聊天室的机制不一样,不覆盖的话功能会异常
    	try{
	    	for (Object room : rooms.toArray()) {
	            left(String.valueOf(room));
	     }
    	}catch(Exception ex){}
    }
}
